#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
####################################################################
#
#   lsswenvir command
#
#   Syntax:  lsswenvir {-f <frame> -g <cage> } | --help }
#
#            -f <frame>      - Specifies the frame for the switch whose
#				environmentals are being queried
#            -g <cage>       - Specifies the cage for the switch whose
#				environmentals are being queried
#            --help          - Prints this message
#
####################################################################
#
#   Initial version - 11/11/02
#
####################################################################

use Getopt::Long;

#
# Include the following Perl script here to help process the i_stub_FS 
# return codes.
#
require "/opt/hsc/bin/i_stub_msg.pl" ;

require "getopts.pl" ;

sub bynumber { $a <=> $b; }

sub exit_routine {
    
     # remove files from /tmp 
     
     exit $ret; 

} # end exit_routine subroutine

################################################################################
#
# start main body of code
#
################################################################################
$PRGRM  = "lsswenvir";
$BIN    = "/opt/hsc/bin"; 
$I_STUB_FS = "/opt/hsc/bin/i_stub_FS";

$USAGE = "Usage:  lsswenvir {-f <frame> -g <cage> } | --help }\n";

# First check for existence of parameters
if ($#ARGV == -1) {
#	print "No parms\n";     #DEBUG
	print $USAGE;
	$ret = 1;
	&exit_routine;
}

# Get parameters used

Getopt::Long::Configure("no_ignore_case");
$result = GetOptions(
        "help" => \$help,
        "f=s" => \$frame,
        "g=s" => \$cage,
);

#print "GetOptions rc is ", $result, " .\n";

if ($result == 0) {
	# Bad result from GetOptions
#	print "Bad result from GetOptions\n";     #DEBUG
	print $USAGE;      #USAGE
	$ret = 1;
	&exit_routine;
}

if ($help) {
	# help was asked for
#	print "help was specified\n";     #DEBUG
	print $USAGE;      #USAGE
	$ret = 0;
	&exit_routine;
}

if (defined $frame) {
	$frame_parm = $frame;
	if ($frame_parm =~ /\D/) {
#		print "Bad -f value - not a number\n";     #DEBUG
		print $USAGE;
		$ret = 1;
		&exit_routine;
	}
}
else {
	# No -f or nothing else was specified after -f
#	print "No -f value\n";     #DEBUG
	print $USAGE;
	$ret = 1;
	&exit_routine;
}

if (defined $cage) {
	$cage_parm = $cage;
	if ($cage_parm =~ /\D/) {
#		print "Bad -g value - not a number\n";     #DEBUG
		print $USAGE;
		$ret = 1;
		&exit_routine;
	}
	else {
		#CHECK ON MAX CAGE VALUE?
	}
}
else {
	# No -g or nothing else was specified after -g
#	print "No -g value\n";     #DEBUG
	print $USAGE;
	$ret = 1;
	&exit_routine;
}

# Now that we've determined that the user specified all the required parms
#      and passed reasonable values with them, issue the i_stub_FS command
@DATA = `$I_STUB_FS pwr -E -f $frame_parm -c $cage_parm`;

# Check return code from i_stub_FS
if (($DATA[0] == 50) || ($DATA[0] == 51)) {
	&i_stub_msg;
	print $I_STUB_MSG;
	$ret = $i_stub_return;
}
else {
	$ret = 0;
	$lines = 0;
	foreach (@DATA) {
		($f1, $f2, $f3, $f4, $f5) = split /\s+/;
		$OUTPUT[$lines]{0} = $f1;
		$OUTPUT[$lines]{1} = $f2;
		$OUTPUT[$lines]{2} = $f3;
		$OUTPUT[$lines]{3} = $f4;
		$OUTPUT[$lines]{4} = $f5;
        	$lines += 1;
	}

	# Print out a header line then output lines
        $OUT_LINE = "                      DCA-F1   DCA-F2";
	print $OUT_LINE."\n";

        $OUT_LINE = "1.8V Voltage          ";
        $DATA_OUT = sprintf "%-8d %-8d", $OUTPUT[0]{0}, $OUTPUT[1]{0};
        $OUT_LINE = $OUT_LINE.$DATA_OUT;
	print $OUT_LINE."\n";

        $OUT_LINE = "1.8V Current          ";
        $DATA_OUT = sprintf "%-8d %-8d", $OUTPUT[0]{1}, $OUTPUT[1]{1};
        $OUT_LINE = $OUT_LINE.$DATA_OUT;
	print $OUT_LINE."\n";

        $OUT_LINE = "3.3V Voltage          ";
        $DATA_OUT = sprintf "%-8d %-8d", $OUTPUT[0]{2}, $OUTPUT[1]{2};
        $OUT_LINE = $OUT_LINE.$DATA_OUT;
	print $OUT_LINE."\n";

        $OUT_LINE = "3.3V Current          ";
        $DATA_OUT = sprintf "%-8d %-8d", $OUTPUT[0]{3}, $OUTPUT[1]{3};
        $OUT_LINE = $OUT_LINE.$DATA_OUT;
	print $OUT_LINE."\n";

        $OUT_LINE = "Internal DCA Temp.    ";
        $DATA_OUT = sprintf "%-8d %-8d", $OUTPUT[0]{4}, $OUTPUT[1]{4};
        $OUT_LINE = $OUT_LINE.$DATA_OUT;
	print $OUT_LINE."\n";

        $OUT_LINE = "Switch Chip Temp. A   ";
        $DATA_OUT = sprintf "%-8d %-8d", $OUTPUT[2]{0}, $OUTPUT[2]{2};
        $OUT_LINE = $OUT_LINE.$DATA_OUT;
	print $OUT_LINE."\n";

        $OUT_LINE = "Switch Chip Temp. B   ";
        $DATA_OUT = sprintf "%-8d %-8d", $OUTPUT[2]{1}, $OUTPUT[2]{3};
        $OUT_LINE = $OUT_LINE.$DATA_OUT;
	print $OUT_LINE."\n";

}

$exit_routine;
